﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Diagnostics;

namespace PacketPasser
{
    static class StreamExtensions
    {
        public static Stream ToStream(this byte[] bytes,int length)
        {
            return new System.IO.MemoryStream(bytes,0,length);
        }
        public static Int32 ReadInt32(this Stream s)
        {
            var buffer = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                buffer[i] = (byte)s.ReadByte();
            }
            return BitConverter.ToInt32(buffer, 0);
        }
        public static Int64 ReadInt64(this Stream s)
        {
            return Read<Int64>(s, 8, BitConverter.ToInt64);
        }
       

        public static string ReadString(this byte[] buffer, int offset)
        {
            
            Int16 len;
            byte[] a = new byte[2];
            a[0] = buffer[offset];
            a[1] = buffer[offset+1];
            len = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(a, 0));
            Debug.WriteLine("chat len returned " + len);
            if (len < 1)
                return string.Empty;
            byte[] b = new byte[len*2];
            if (buffer.Length - offset - 2 < len)
                return string.Empty;
            for (int i = 0; i <b.Length; i++)
            {
                b[i] = buffer[offset+2+i];
            }
            return Encoding.BigEndianUnicode.GetString(b);
            
        }



        static Int32 ReadInt(byte[] packet, int offset)
        {
            return BitConverter.ToInt32(packet, offset);
        }
        static byte[] ReadBytes(this Stream s, int size)
        {
            var result = new byte[size];
            s.Read(result, 0, size);
            return result;
        }
        static T Read<T>(System.IO.Stream s, int size, Func<byte[], int, T> converter)
        {
            var bytes = s.ReadBytes(size);
            var result = converter(bytes, 0);
            return result;
        }

        static Object Read(System.IO.Stream s, int num)
        {
            byte[] b = new byte[num];
            for (int i = 0; i < b.Length; i++)
            {
                b[i] = (byte)s.ReadByte();
            }
            switch (num)
            {
                case 4:
                    return BitConverter.ToInt32(b, 0);
                case 8:
                    return BitConverter.ToInt64(b, 0);
                case 2:
                    return BitConverter.ToInt16(b, 0);
                default:
                    return 0;
            }
        }

        internal static String ReadString(this System.IO.Stream s)
        {
            Int16 len;
            
            byte[] a = new byte[2];
            a[0] = (byte)s.ReadByte();
            a[1] = (byte)s.ReadByte();
            len = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(a, 0));
            if (len < 1)
            {
                var oldColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine();
                Console.WriteLine("failed to read a packet len was " + len);
                Console.ForegroundColor = oldColor;
                return string.Empty;
            }
            byte[] b = new byte[len * 2];
            for (int i = 0; i < len * 2; i++)
            {
                b[i] = (byte)s.ReadByte();
                Debug.Assert(b[i] != -1);
            }
            return Encoding.BigEndianUnicode.GetString(b);
        }
    }
}
